﻿using System;
using System.Collections.Generic;
using System.Linq;
using Model;
using DBUtility;
using MySql.Data.MySqlClient;

namespace DAL
{
    public class EquipmentRepository
    {
        // 添加设备
        public int AddEquipment(Equipment equipment)
        {
            string sql = @"INSERT INTO equipment 
                            (EquipmentDescription, Status, CommunicationStatus, LockStatus, 
                             EquipmentStatus, EquipmentType, EquipmentModel, DetailedEquipmentType, 
                             WaitingCapacity, ProcessCapacity, LoadingCapacity) 
                           VALUES 
                            (@EquipmentDescription, @Status, @CommunicationStatus, @LockStatus, 
                             @EquipmentStatus, @EquipmentType, @EquipmentModel, @DetailedEquipmentType, 
                             @WaitingCapacity, @ProcessCapacity, @LoadingCapacity)";

            MySqlParameter[] parameters = {
                new MySqlParameter("@EquipmentDescription", equipment.EquipmentDescription),
                new MySqlParameter("@Status", equipment.Status),
                new MySqlParameter("@CommunicationStatus", equipment.CommunicationStatus),
                new MySqlParameter("@LockStatus", equipment.LockStatus),
                new MySqlParameter("@EquipmentStatus", equipment.EquipmentStatus),
                new MySqlParameter("@EquipmentType", equipment.EquipmentType),
                new MySqlParameter("@EquipmentModel", equipment.EquipmentModel),
                new MySqlParameter("@DetailedEquipmentType", equipment.DetailedEquipmentType),
                new MySqlParameter("@WaitingCapacity", equipment.WaitingCapacity),
                new MySqlParameter("@ProcessCapacity", equipment.ProcessCapacity),
                new MySqlParameter("@LoadingCapacity", equipment.LoadingCapacity)
            };

            return DBHelper.ExecuteNonQuery(sql, parameters);
        }

        // 删除设备
        public int DeleteEquipment(int equipmentId)
        {
            string sql = "DELETE FROM equipment WHERE EquipmentId = @EquipmentId";
            MySqlParameter[] parameters = { new MySqlParameter("@EquipmentId", equipmentId) };
            return DBHelper.ExecuteNonQuery(sql, parameters);
        }

        // 更新设备信息
        public int UpdateEquipment(Equipment equipment)
        {
            string sql = @"UPDATE equipment 
                            SET EquipmentDescription = @EquipmentDescription, 
                                Status = @Status, 
                                CommunicationStatus = @CommunicationStatus, 
                                LockStatus = @LockStatus, 
                                EquipmentStatus = @EquipmentStatus, 
                                EquipmentType = @EquipmentType, 
                                EquipmentModel = @EquipmentModel, 
                                DetailedEquipmentType = @DetailedEquipmentType, 
                                WaitingCapacity = @WaitingCapacity, 
                                ProcessCapacity = @ProcessCapacity, 
                                LoadingCapacity = @LoadingCapacity 
                          WHERE EquipmentId = @EquipmentId";

            MySqlParameter[] parameters = {
                new MySqlParameter("@EquipmentDescription", equipment.EquipmentDescription),
                new MySqlParameter("@Status", equipment.Status),
                new MySqlParameter("@CommunicationStatus", equipment.CommunicationStatus),
                new MySqlParameter("@LockStatus", equipment.LockStatus),
                new MySqlParameter("@EquipmentStatus", equipment.EquipmentStatus),
                new MySqlParameter("@EquipmentType", equipment.EquipmentType),
                new MySqlParameter("@EquipmentModel", equipment.EquipmentModel),
                new MySqlParameter("@DetailedEquipmentType", equipment.DetailedEquipmentType),
                new MySqlParameter("@WaitingCapacity", equipment.WaitingCapacity),
                new MySqlParameter("@ProcessCapacity", equipment.ProcessCapacity),
                new MySqlParameter("@LoadingCapacity", equipment.LoadingCapacity),
                new MySqlParameter("@EquipmentId", equipment.EquipmentId)
            };

            return DBHelper.ExecuteNonQuery(sql, parameters);
        }

        // 查询所有设备
        public List<Equipment> GetAllEquipments()
        {
            string sql = "SELECT * FROM equipment";
            return DBHelper.ExecuteQuery(sql, null, MapEquipment);
        }

        // 查询单个设备
        public Equipment GetEquipmentById(int equipmentId)
        {
            string sql = "SELECT * FROM equipment WHERE EquipmentId = @EquipmentId";
            MySqlParameter[] parameters = { new MySqlParameter("@EquipmentId", equipmentId) };
            var equipments = DBHelper.ExecuteQuery(sql, parameters, MapEquipment);
            return equipments.FirstOrDefault();
        }

        // 映射数据库查询结果到 Equipment 对象
        private Equipment MapEquipment(MySqlDataReader reader)
        {
            return new Equipment
            {
                EquipmentId = (int)reader["EquipmentId"],
                EquipmentDescription = reader["EquipmentDescription"] is DBNull ? null : reader["EquipmentDescription"].ToString(),
                Status = reader["Status"].ToString(),
                CommunicationStatus = reader["CommunicationStatus"].ToString(),
                LockStatus = reader["LockStatus"].ToString(),
                EquipmentStatus = reader["EquipmentStatus"].ToString(),
                EquipmentType = reader["EquipmentType"].ToString(),
                EquipmentModel = reader["EquipmentModel"].ToString(),
                DetailedEquipmentType = reader["DetailedEquipmentType"].ToString(),
                WaitingCapacity = (int)reader["WaitingCapacity"],
                ProcessCapacity = (int)reader["ProcessCapacity"],
                LoadingCapacity = (int)reader["LoadingCapacity"]
            };
        }
    }
}